/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include.h"
#include "main_window.h"
#include "main_window_sqlite.h"
#include "main_window_notebook.h"
#include "util.h"

void
main_window_controls_set_enabled(GtkBuilder *builder, gboolean state)
{
	GtkWidget *toolbar, *notebook;

	D_ASSERT(builder != NULL);
	
	toolbar = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar"));
	D_ASSERT(toolbar != NULL);
	gtk_widget_set_sensitive(toolbar, state);
	
	notebook = GTK_WIDGET(gtk_builder_get_object(builder, "notebook"));
	D_ASSERT(notebook != NULL);
	gtk_widget_set_sensitive(notebook, state);
}

GtkBuilder*
main_window_create(const gchar *db_path, const GList *sql_paths)
{
	GtkBuilder *builder;
	gint result;
	GtkWindow *window;
	
	builder = gtk_builder_new();
	D_ASSERT(builder);
	
	result = gtk_builder_add_from_file(builder, "main_window.xml", NULL);
	
	D_ASSERT(result);
	
	/* pass "builder" to all functions as the gpointer user_data */
	gtk_builder_connect_signals(builder, builder);
	
	window = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
	D_ASSERT(window);
	
	/* Assign the builder as a data object to allow easy access to it if a pointer to *window is available */
	g_object_set_data_full (G_OBJECT(window), "builder", builder, (GDestroyNotify)g_object_unref);
	
	if(prefs_manager_get_bool(prefs_manager, "main_window", "left_tree_view_visible", TRUE))
	{
		GtkPaned *paned = GTK_PANED(gtk_builder_get_object(builder, "hpaned"));
		D_ASSERT(paned);
		
		gtk_paned_set_position(paned, prefs_manager_get_int(prefs_manager, "main_window", "hpaned_position", 100));
	}
	else
	{
		GtkWidget *widget = GTK_WIDGET(gtk_builder_get_object(builder, "treeview_table"));
		D_ASSERT(widget);
		
		gtk_widget_hide(widget);
	}
	
	gtk_widget_show(GTK_WIDGET(window));
	
	/* This keeps a count of the number of active windows. If active windows == 0 on any window destruction, it calls the gtk_main_quit(); */
	total_windows++;
	
	main_window_controls_set_enabled(builder, FALSE);
	
	if(db_path)
		main_window_sqlite_open_database(builder, db_path);
	
	return builder;
}

gboolean
main_window_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
	/* delete_event return wants the return of whether to CANCEL the event or not. close_all_tabs returns the success of the close_tabs */
	return !main_window_notebook_close_all_tabs(main_window_notebook_get_notebook_from_user_data(user_data), user_data);
}

void
main_window_destroy_cb (GtkObject *object, gpointer user_data)
{
	GtkBuilder *builder;
	GtkWindow *window;
	
	g_assert(object);
	g_assert(user_data);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder);
	
	window = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
	g_assert(window);
	
	/* So, apparently, when your objects are destroyed, their GDestroyNotify for set_data_full are not called. 
	   Awesome! A workaround that should manually fire the destructor */
	g_object_set_data(G_OBJECT(window), "sqlite_database", NULL);
	
	gtk_widget_destroy(GTK_WIDGET(window));
	
	/* Decrement the total_windows to keep a count of how many active windows there are. If the active == 0, call the main_quit(); */
	if(likely(!--total_windows))
		gtk_main_quit();
}
